class ShippingsController < ApplicationController
  before_action :set_shipping, only: [:show, :edit, :update, :destroy]
  before_action :set_page_title
  
  
  # GET /shippings
  # GET /shippings.json
  def index
    @shippings = Shipping.order(created_at: :desc).page(params[:page])
  end

  # GET /shippings/1
  # GET /shippings/1.json
  def show
  end

  # GET /shippings/new
  def new
    @order = set_order
    @shipping = Shipping.new
  end

  # GET /shippings/1/edit
  def edit
    @order = set_order
  end

  # POST /shippings
  # POST /shippings.json
  def create
    @shipping = Shipping.new(shipping_params)
    @order = Order.find(@shipping.order_id) if @shipping.order_id.present?

    respond_to do |format|
      if @shipping.save
        format.html { redirect_to @shipping, notice: 'Shipping was successfully created.' }
        format.json { render :show, status: :created, location: @shipping }
      else
        format.html { render :new }
        format.json { render json: @shipping.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /shippings/1
  # PATCH/PUT /shippings/1.json
  def update
    respond_to do |format|
      if @shipping.update(shipping_params)
        format.html { redirect_to shippings_path, notice: 'Shipping was successfully updated.' }
        format.json { render :show, status: :ok, location: @shipping }
      else
        format.html { render :edit }
        format.json { render json: @shipping.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /shippings/1
  # DELETE /shippings/1.json
  def destroy
    @shipping.destroy
    respond_to do |format|
      format.html { redirect_to shippings_path, notice: 'Shipping was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_shipping
    @shipping = Shipping.find(params[:id])
  end

  def set_order
    # if params[:order_id].present?
      # @order = Order.find(params[:order_id])   
    # end
    
    @order = params[:order_id].present? ? Order.find(params[:order_id])  : nil 
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def shipping_params
    params.require(:shipping).permit(:date, :method, :carrier, :tracking, :comment, :order_id,:weight, :dimension,:price,:receiver, :receiver_contact,:receiver_tel, :receiver_address,:waybill_image )
  end

  def set_page_title
    @page_title = "物流信息管理"
  end
end
